// Base64转PDF文件
export default {
  base64ToBlob(code, type) {
    code = code.replace(/[\n\r]/g, "");
    let raw = window.atob(code);
    let rawLength = raw.length;
    let uint8Array = new Uint8Array(rawLength);
    for (let i = 0; i < rawLength; i++) {
      uint8Array[i] = raw.charCodeAt(i);
    }
    if ("pdf" === type) {
      return new Blob([uint8Array], {
        type: "application/pdf"
      });
    } else {
      return new Blob([uint8Array], {
        type: "image/jpeg"
      });
    }
  },

  base64FileHeaderMapper(fileBase64) {
    let fileHeader = new Map();
    //获取不同文件的文件头前3个字作为判断依据
    fileHeader.set("/9j", "jpeg");
    fileHeader.set("iVB", "png");
    // fileHeader.set("Qk0", "BMP")
    // fileHeader.set("SUk", "TIFF")
    fileHeader.set("JVB", "pdf");
    // fileHeader.set("UEs", "OFD")
    let res = "";
    //遍历map中所提及的文件头特征
    fileHeader.forEach((v, k) => {//箭头函数IE不支持 兼容IE需改写
      if (k == fileBase64.substr(0, 3)) {
        res = v;
      }
    });
    //如果不在map中返回unknown file
    if (res == "") {
      res = "unknown file";
    }
    //否则返回map中的value值
    return res;
  },

// 订单金额计算
  countPrice(val, ...value) {
    // console.log('value', val, value)
    val = Number(val)
    if (!val) {
      val = 0
    }
    // return val.toFixed(2).split(".")
    return val.toFixed(2)
  }
}
